package com.hsource.doctorcenter.service.api.impl;

import com.alibaba.fastjson.JSONObject;
import com.hsource.doctorcenter.service.api.IApiService;
import com.hsource.doctorcenter.util.SignParams;
import com.hsource.doctorcenter.util.StaticValue;
import com.hsource.doctorcenter.util.http.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.Map;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/9/24 13:27
 * @describe 调用第三方API
 */
@Service
@Slf4j
public class ApiServiceImpl implements IApiService {

    @Value("${chunyutianxia.api-url}")
    private String apiUrl;

    /**
     * 获取问题详情
     * @param problemId
     * @param userId
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String getProblemDetail(String problemId,String userId) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("problem_id", problemId);

        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/problem/detail", signParams.getParams());
    }

    /**
     * 退款接口
     * @param paramsMap 退款参数
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public boolean refundApi(Map<String,String> paramsMap) {
        try {
            String s = OkHttpUtil.postFormParams(StaticValue.PAYCENTER_API_URL + "/pay/refund", paramsMap);
            JSONObject jsonObject = JSONObject.parseObject(s);
            return jsonObject.getBooleanValue("success");
        }catch (Exception e){
        }
        return false;
    }
    /**
     * 获取医生可预约时间|时长价格
     * @param doctorId 医生Id
     * @param userId 用户Id
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String getDoctorPhoneInfo(String doctorId,String userId) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("doctor_id", doctorId);

        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/phone/get_doctor_phone_info/", signParams.getParams());
    }

    /**
     * 创建快捷电话问题接口
     * @param userId 用户Id
     * @param content 问题内容
     * @param clinicNo 科室
     * @param phone 电话
     * @param partnerOrderId 合作方支付ID
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String createFastPhone(String userId,String content,String clinicNo,String partnerOrderId,String phone) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("content", content)
                .paramEntry("clinic_no", clinicNo)
                .paramEntry("phone", phone)
                .paramEntry("partner_order_id", partnerOrderId);

        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/phone/create_fast_phone_order/", signParams.getParams());
    }

    /**
     * 问题追问
     * @param userId 用户id
     * @param content 内容
     * @param problemId 问题id
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String problemContent(String userId,String content,String problemId) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("content", content)
                .paramEntry("problem_id", problemId);

        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/problem_content/create", signParams.getParams());
    }
    /**
     * 创建图文急诊问题接口
     * @param userId 用户Id
     * @param content 问题内容
     * @param clinicNo 科室
     * @param partnerOrderId 合作方支付ID
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String createEmergencyGraph(String userId,String content,String clinicNo,String partnerOrderId) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("content", content)
                .paramEntry("clinic_no", clinicNo)
                .paramEntry("partner_order_id", partnerOrderId);

        log.warn("createEmergencyGraph================="+JSONObject.toJSONString(signParams.getParams()));
        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/problem/create_emergency_graph/", signParams.getParams());
    }

    /**
     * 创建定向电话
     * @param userId 用户Id
     * @param content 问题内容
     * @param price 价格
     * @param doctorIds 指定医生 使用#进行连接多个医生，不能有空格
     * @param partnerOrderId 合作方支付ID
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String createOrientedOrder(String userId,String doctorIds,String content,Integer price,String partnerOrderId,String phone,String inquiryTime,int minutes) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("content", content)
                .paramEntry("price", String.valueOf(price))
                .paramEntry("partner_order_id", partnerOrderId)
                .paramEntry("tel_no", phone)
                .paramEntry("minutes", String.valueOf(minutes))
                .paramEntry("inquiry_time", inquiryTime)
                .paramEntry("doctor_id", doctorIds);

        log.warn("createOrientedOrder================="+JSONObject.toJSONString(signParams.getParams()));
        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/phone/create_oriented_order/", signParams.getParams());
    }

    /**
     * 创建快速提问问题-定向医生提问
     * @param userId 用户Id
     * @param content 问题内容
     * @param price 价格
     * @param doctorIds 指定医生 使用#进行连接多个医生，不能有空格
     * @param partnerOrderId 合作方支付ID
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String createOrientedProblem(String userId,String doctorIds,String content,Integer price,String partnerOrderId) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("content", content)
                .paramEntry("price", String.valueOf(price))
                .paramEntry("partner_order_id", partnerOrderId)
                .paramEntry("doctor_ids", doctorIds);

        log.warn("createOrientedProblem================="+JSONObject.toJSONString(signParams.getParams()));
        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/problem/create_oriented_problem/", signParams.getParams());
    }


    /**
     * 创建快速提问问题（众包升级）-系统分配
     * @param userId 用户Id
     * @param content 问题内容
     * @param payType 付费升级类型
     * @param partnerOrderId 合作方支付ID
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String createPaidProblem(String userId,String content,String payType,String partnerOrderId) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("content", content)
                .paramEntry("partner_order_id", partnerOrderId)
                .paramEntry("pay_type", payType);
        log.warn("createPaidProblem================="+JSONObject.toJSONString(signParams.getParams()));
        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/problem/create_paid_problem/", signParams.getParams());
    }

    /**
     * 提问历史
     * @param userId 用户Id
     * @param startNum 开始数 用于支持翻页功能,从 0 开始计数
     * @param count 每次取的问题数 最大值200
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String historicalQuestions(String userId,Integer startNum,Integer count) throws ParseException, NoSuchAlgorithmException{
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("start_num", String.valueOf(startNum))
                .paramEntry("count", String.valueOf(count));
        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/problem/list/my", signParams.getParams());
    }

    /**
     * 获取图文急诊科室信息
     * @param userId
     * @return
     */
    @Override
    public String getImageTextAndPhoneClinics(String userId,String clinicType) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId);
        if(clinicType.equals("imagetext")){
            return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/problem/get_emergency_graph_info/", signParams.getParams());
        }
        if(clinicType.equals("phone")){
            return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/phone/get_fast_phone_info/", signParams.getParams());
        }
        return "";
    }

    /**
     * 获取医生详情
     * @param userId 用户id
     * @param doctorId 医生id
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @Override
    public String getDoctorsDetailed(String userId,String doctorId) throws ParseException, NoSuchAlgorithmException{
        SignParams signParams = SignParams.newBuilder(userId).paramEntry("doctor_id", doctorId);
        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/doctor/detail", signParams.getParams());
    }

    /**
     * 推荐医生
     * @param userId 用户标识
     * @param ask 首次提问内容文本
     * @return
     */
    @Override
    public String recommendedDoctors(String userId,String ask) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId).paramEntry("ask", ask);
        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/doctor/get_recommended_doctors", signParams.getParams());
    }

    /**
     * 获取医生列表
     * @param userId 用户id
     * @param famousDoctor 是否是名医
     * @param startNum 开始数
     * @param count 页加载数量
     * @param province 省份
     * @param city 城市
     * @param serviceType 服务类型
     * @param clinicNo 科室编码
     * @return
     */
    @Override
    public String getDoctors(String userId,int famousDoctor,int startNum,int count,String province,String city,String serviceType,String clinicNo) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId)
                .paramEntry("start_num", String.valueOf(startNum))
                .paramEntry("count", String.valueOf(count))
                .paramEntry("service_type", serviceType)
                .paramEntry("famous_doctor", String.valueOf(famousDoctor))
                .paramEntry("province", province)
                .paramEntry("city", city)
                .paramEntry("clinic_no", clinicNo);
        //调用接口返回数据
        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/doctor/get_clinic_doctors", signParams.getParams());
    }

    /**
     * 搜索医生接口
     * @param queryText 搜索词
     * @param page 页码 1-10
     * @param province 省份
     * @param city 城市
     * @return
     */
    @Override
    public String searchDoctors(String userId,String queryText,int page,String province,String city) throws ParseException, NoSuchAlgorithmException {
        SignParams signParams = SignParams.newBuilder(userId);
        //调用接口返回数据
        return OkHttpUtil.postFormParams(apiUrl+"/cooperation/server/doctor/search_doctor/",  signParams.getParams());
    }

}
